//
//  WTChatRoomCell.m
//  66TV
//
//  Created by ben on 16/8/1.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTChatRoomCell.h"
#import "Masonry.h"
#import "UIColor+WTExtend.h"
#import "UIImage+WTExtend.h"



@interface WTChatRoomCell ()
@property (nonatomic, strong) UIView*       levelBgView;
@property (nonatomic, strong) UIImageView*  levelIconImageView;
@property (nonatomic, strong) UILabel*      levelNumberLabel;
@property (nonatomic, strong) UILabel*      contentLabel;

@end





@implementation WTChatRoomCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
    
        self.backgroundColor = [UIColor clearColor];
        [self setup];
    }
    return self;
}


#pragma mark - 
- (void)setup{
    
    self.levelBgView = ({
        UIView* view = [[UIView alloc] init];
        view.layer.cornerRadius = 4;
        view.layer.masksToBounds = YES;
        view.backgroundColor = [UIColor hexChangeFloat:@"FF7864"];
        [self.contentView addSubview:view];
        view;
    });
    
    self.levelIconImageView = ({
        
        UIImageView*    iconImageView = [[UIImageView alloc] init];
        iconImageView.image = [UIImage wt_imageWithName:@"wt_level"];
        [self.levelBgView addSubview:iconImageView];
        iconImageView;
    });
    
    self.levelNumberLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.numberOfLines = 1;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont boldSystemFontOfSize:8];
        label.text = @"12";
        [self.levelBgView addSubview:label];
        label;
    });
    
    self.contentLabel = ({
        UILabel* label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor clearColor];
        label.numberOfLines = 0;
        [self.contentView addSubview:label];
        label;
    });

    
    [self layout];
}

#pragma mark - 
- (void)layout{
    
    [self.levelBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(6);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(12);
        make.top.mas_equalTo(5);
    }];
    
    [self.levelIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(6);
        make.centerY.mas_equalTo(self.levelBgView.mas_centerY);
        make.height.and.width.mas_equalTo(10);
    }];
    
    [self.levelNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.levelIconImageView.mas_trailing).offset(0);
        make.bottom.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
    }];
    
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.levelBgView.mas_leading);
        make.top.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)setMessageModel:(WTBaseMessageModel *)messageModel{
    

    NSMutableParagraphStyle* defaultParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    defaultParagraphStyle.firstLineHeadIndent = 35;
    defaultParagraphStyle.lineSpacing = 4;
    NSString* text = [NSString stringWithFormat:@"%@ %@",messageModel.chat_room_message_avtar_name,messageModel.chat_room_message_text];
    NSDictionary* attribute = @{NSParagraphStyleAttributeName:defaultParagraphStyle,NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSMutableAttributedString* string = [[NSMutableAttributedString alloc] initWithString:text attributes:attribute];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor hexChangeFloat:@"b4b4b4"] range:[text rangeOfString:messageModel.chat_room_message_avtar_name]];
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:[text rangeOfString:messageModel.chat_room_message_text]];
    self.contentLabel.attributedText = string;
    self.levelNumberLabel.text = [NSString stringWithFormat:@"%zd",messageModel.chat_room_message_level];

    
    
}



@end
